package com.lxx.codegen.mbgplugins;

import java.util.List;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

public class BatchDelPlugin extends PluginAdapter{

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}
	
	@Override
	public boolean clientGenerated(Interface interfaze,
			TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//		Method m = new Method("query");
//		m.setVisibility(JavaVisibility.PUBLIC);
//		m.setReturnType(FullyQualifiedJavaType.getNewListInstance());
//		context.getCommentGenerator().addGeneralMethodComment(m,
//				introspectedTable);
//		interfaze.addMethod(m);
		return true;
	}
	
	@Override
	public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
		
		String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();//数据库表名  
		
		XmlElement parentElement = document.getRootElement();

		// 产生分页语句前半部分
		XmlElement queryElement = new XmlElement("delete");
		queryElement.addAttribute(new Attribute("id", "batchDel"));
		
		queryElement.addElement(
				new TextElement(
				"delete from "+tableName + " where id in \n"
						+ "    <foreach item=\"item\" index=\"index\" collection=\"array\"      open=\"(\" separator=\",\" close=\")\">\n"
						+ "        #{item}  \n"
						+ "    </foreach>"
				));
		
		parentElement.addElement(queryElement);

		return super.sqlMapDocumentGenerated(document, introspectedTable);
	}
	

}
